<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:seal:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:seal:edit']"
        >修改</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="sealList" @selection-change="handleSelectionChange">
      <el-table-column width="55" align="center" />
      <el-table-column label="通用印章ID" align="center" prop="sealId" />
      <el-table-column label="通用印章名称" align="center" prop="sealName" />
      <el-table-column label="印章说明" align="center" prop="sealIllustrate" />
      <el-table-column
        label="印章文件图片"
        prop="sealUrl"
        header-align="center"
      >
      <template slot-scope="scope">
        <div  style="display: flex;justify-content: center;">
          <img :src="`${src1 +scope.row.sealUrl}`"  width=50px height=50px/>
        </div>
      </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:seal:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户印章对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item  label="印章名称" prop="sealName">
          <el-input  v-model="form.sealName" placeholder="请输入印章名称" />
        </el-form-item>
        <el-form-item label="印章说明" prop="sealIllustrate">
          <el-input  v-model="form.sealIllustrate" placeholder="请输入印章说明" />
        </el-form-item>
        <el-form-item label="印章图片" prop="sealUrl">
        <el-upload
          limit=1
          :action="actionImg"
          :auto-upload="false"
          ref="imageUpload"
          list-type="picture-card"
          :headers="headers"
          :file-list="fileList"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :on-change="onChange"
          :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSeal, updateSeal, uploadCommonSeal } from "@/api/seal";
import { getToken } from "@/utils/auth";
import module from "@/ldi/lk";

export default {
  name: "Seal",
  
  data() {
    return {
      src1 : '',
      //
      // uploadHeaders:{"token":localStorage.getItem('token')},
      // token: localStorage.getItem('token'),
      // Authorization:"",
      //
      dialogImageUrl: '',
      dialogVisible: false,
      //action路径
      actionImg:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户印章表格数据
      sealList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        userId: null,
        sealUrl: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // actionImg :
      //         process.env.VUE_APP_BASE_API +
      //         `/business/seal/uploadCommonSeal` +
      //         "?" +
      //         "sealName=" +
      //         this.form.sealName +
      //         "&" +
      //         "sealIllustrate=" +
      //         this.form.sealIllustrate +
      //         "&" +
      //         "sealId=" +
      //         this.form.sealId,

      headers: {
        // Authorization:""
        Authorization: "Bearer " + getToken(),
        // Authorization: "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjNlMjUyZTgyLTYzMWQtNGM1MS05ZTNkLTAwYzZmMjYxODAzZiJ9.2FUUcQ2bRJaAdUXtNoklJP9-HulYY33g47bNskvw6givNEpxDy0-OKdwESXA4E3r6JX016zsSD2hIN4-ItPrmA" 
      },
      
      //图片列表
      fileList: [],
      //修改判断
      imageLen: 0,

    };
  },
  created() {
    this.src1 = module.host;
    this.getList();
    // this.headers.Authorization= "Bearer " +getToken();
    // this.ausorToken.Authorization = 'Bearer ' + getToken();
  },
  methods: {
    // 无论成功失败都会触发
    onChange(uploadFile, fileList){
      // this.ausorToken.Authorization='Bearer ' + getToken();
      this.imageLen = fileList.length;
    },
    handleSuccess(response, file, fileList, rowInfo) {
      console.log(response, file, fileList, rowInfo);
      // this.ausorToken.Authorization='Bearer ' + getToken();
    },
    uploadTimeOut(){
      this.$refs.imageUpload.submit();
    },
    handleRemove(file, fileList) {
        this.imageLen = 0;
        console.log(fileList.length);
        console.log(file, fileList);
      },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
      console.log("aaa");
    },
    /** 查询用户印章列表 */
    getList() {
      this.loading = true;
      listSeal(this.queryParams).then(response => {
        this.sealList = response.data.通用印章;
        this.total = response.total+0;
        this.loading = false;
        // console.log(this.sealList)
      });
    },
    // 取消按钮
    cancel() {
      this.imageLen = 0;
      console.log(this.fileList.length);
      this.open = false;
      this.reset();
      this.getList();
    },
    // 表单重置
    reset() {
      this.form = {
        sealId: null,
        sealName: null,
        sealUrl: null,
        sealIllustrate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sealId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户印章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // const sealId = row.sealId || this.ids
      // updateSeal(sealId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改用户印章";
      // });
      this.imageLen=this.fileList.length;
      this.form=row;
      // console.log(this.form.sealIllustrate)
      this.form.sealUrl="http://localhost/dev-api"+this.form.sealUrl;
      this.open = true;
      this.title = "修改用户印章";
    },
    /** 提交按钮 */
    submitForm() {
      
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sealId != null) {
            // this.actionImg =
            //   process.env.VUE_APP_BASE_API +
            //   `/business/seal/uploadCommonSeal` +
            //   "?" +
            //   "sealId=" +
            //   this.from.sealId +
            //   "&"
            //   "sealName=" +
            //   this.form.sealName +
            //   "&" +
            //   "sealIllustrate=" +
            //   this.form.sealIllustrate;
            if(this.imageLen != 0){
              // uploadCommonSeal({sealId : this.form.sealId, sealName : this.form.sealName, sealIllustrate:this.form.sealIllustrate}).then(response => {
              //   console.log(response)
              // })
              // console.log(upload)
              // this.ausorToken.Authorization='Bearer ' + getToken();
              this.actionImg =
              process.env.VUE_APP_BASE_API +
              `/business/seal/uploadCommonSeal` +
              "?" +
              "sealName=" +
              this.form.sealName +
              "&" +
              "sealIllustrate=" +
              this.form.sealIllustrate +
              "&" +
              "sealId=" +
              this.form.sealId;
              setTimeout(this.uploadTimeOut,1000);
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              setTimeout(this.getList,2000)
            } else{
              updateSeal({sealId : this.form.sealId, sealName : this.form.sealName, sealIllustrate:this.form.sealIllustrate});
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList;
            }
          } else {
            this.actionImg =
              process.env.VUE_APP_BASE_API +
              `/business/seal/uploadCommonSeal` +
              "?" +
              "sealName=" +
              this.form.sealName +
              "&" +
              "sealIllustrate=" +
              this.form.sealIllustrate;
            setTimeout(this.uploadTimeOut,1000);
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            setTimeout(this.getList,5000)
          }
        }
      });
    },
  }
};
</script>
